package com.cndw.kungfu.model.goods;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.cndw.commons.util.BlankUtils;
import com.cndw.commons.util.DateFormatUtils;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.vo.EventNotice;

/**
 * 摆摊对象
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on 2011-9-28
 */
public class PlayerMarket {

	/**
	 * 摊主id
	 */
	private long playerId;

	/**
	 * 摊主名字
	 */
	private String playerName;

	/**
	 * 摊位名
	 */
	private String marketName;

	/**
	 * 公告
	 */
	private String notice;

	/**
	 * 开始摆摊时间
	 */
	private int beginTime;

	/**
	 * 背包位置，摊位位置，价钱，交易类型(1:银两，2：金币),数量}
	 */
	private Map<Integer, Integer[]> priceMap = new HashMap<Integer, Integer[]>();

	/**
	 * 留言(最多20条){时间,留言者,消息}
	 */
	private LinkedList<String[]> messages = new LinkedList<String[]>();

	/**
	 * 销售记录{时间，购买者，物品，数量，价钱,类型}
	 */
	private List<String[]> saleRecord = new ArrayList<String[]>();

	public PlayerMarket(long playerId, String playerName, String marketName, String notice) {
		this.playerId = playerId;
		this.playerName = playerName;
		this.marketName = marketName;
		this.notice = notice;
		this.beginTime = TimeProcesser.getUnixTime();
	}

	/**
	 * 增加物品
	 * 
	 * @param storage
	 * @param price
	 */
	public void addGoods(int storage, Integer[] price) {
		priceMap.put(storage, price);
	}

	/**
	 * 下架物品(不会记录销售情况)
	 * 
	 * @param storage
	 */
	public Integer[] removeGoods(int storage) {
		return priceMap.remove(storage);
	}

	/**
	 * 获取销售数量
	 * 
	 * @return
	 */
	public int getGoodsSize() {
		return priceMap.size();
	}

	/**
	 * 获取物品
	 * 
	 * @param storage
	 * @return
	 */
	public Integer[] getGoods(int storage) {
		return priceMap.get(storage);
	}

	/**
	 * 判断是否存在
	 * 
	 * @param storage
	 * @return
	 */
	public boolean constain(int storage) {
		return priceMap.containsKey(storage);
	}

	/**
	 * 留言
	 * 
	 * @param playerName
	 * @param msg
	 */
	public synchronized void addMessage(String playerName, String msg) {

		if (messages.size() >= ConstantsModel.marketMaxMessage) {
			messages.removeFirst();
		}

		messages.add(new String[] { TimeProcesser.getCUnixTime(), playerName, msg });
	}

	/**
	 * 出售物品
	 * 
	 * @param storage
	 * @param counts
	 * @param playerName
	 * @param goodsName
	 */
	public synchronized void saleGoods(int storage, int counts, String playerName, String goodsName) {

		// 移除物品
		Integer[] price = priceMap.get(storage);

		if (!BlankUtils.isBlank(price)) {

			if (price[4] == counts) {
				priceMap.remove(storage);
			}

			price[4] = price[4] - counts;

			// 增加销售记录
			saleRecord.add(new String[] { TimeProcesser.getCUnixTime(), playerName, goodsName, String.valueOf(counts), price[2].toString(), price[3].toString() });
		}
	}

	/**
	 * 收摊
	 */
	public void closeMarket() {

		// 邮件统计销售记录
		Player player = Context.getPlayerServer().getByPlayerId(playerId);

		if (!saleRecord.isEmpty()) {

			String title = LanguageLib.get("market.sale.title");

			StringBuilder content = new StringBuilder();
			content.append(LanguageLib.get("market.sale.beginTime"));
			content.append(DateFormatUtils.formatDateTime(new Date(beginTime * 1000L)));
			content.append("<br/>");
			content.append(LanguageLib.get("market.sale.info"));
			content.append("<br/>");
			for (String[] record : saleRecord) {
				content.append("&nbsp;&nbsp;&nbsp;&nbsp;");
				// {时间，购买者，物品，数量，价钱,类型}
				content.append(DateFormatUtils.formatDateTime(new Date(Integer.parseInt(record[0]) * 1000L)));
				content.append("--");
				content.append(record[1]);
				content.append("&nbsp;&nbsp;");
				content.append(record[2]);
				content.append(":&nbsp;&nbsp;");
				content.append(record[3]);
				content.append("&nbsp;&nbsp;*&nbsp;&nbsp;");
				content.append(record[4]);
				content.append(LanguageLib.get(Integer.parseInt(record[5]) == 1 ? "market.sale.type1" : "market.sale.type2"));
				content.append("<br/>");
			}
			content.append(LanguageLib.get("market.sale.endTime"));
			content.append(DateFormatUtils.formatDateTime(new Date()));

			Context.getMessageServer().sendSysMail(playerId, player.getPlayerName(), title, content.toString());
		}

		player.stateRemove(Constants.playerState64_market);

		// 解锁所有物品
		for (Integer[] r : priceMap.values()) {
			player.getPlayerBoxs().unlockGoods(r[0]);
		}

		// 广播状态
		Context.getPlayerServer().broadcaseState(player);

		priceMap.clear();
		messages.clear();
		saleRecord.clear();
	}

	public void broadcaseMarketName() {
		EventNotice notice = new EventNotice("marketObject", "updateName");
		notice.put("playerId", getPlayerId());
		notice.put("name", getMarketName());
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		notice.broadcastScene(player.getSceneId(), player.getSubLine());
	}

	public void broadcaseMarketNotice() {
		EventNotice notice = new EventNotice("marketObject", "updateNotice");
		notice.put("playerId", getPlayerId());
		notice.put("notice", getNotice());
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		notice.broadcastScene(player.getSceneId(), player.getSubLine());
	}

	public long getPlayerId() {
		return playerId;
	}

	public void setPlayerId(long playerId) {
		this.playerId = playerId;
	}

	public String getPlayerName() {
		return playerName;
	}

	public void setPlayerName(String playerName) {
		this.playerName = playerName;
	}

	public String getMarketName() {
		return marketName;
	}

	public void setMarketName(String marketName) {
		this.marketName = marketName;
	}

	public String getNotice() {
		return notice;
	}

	public void setNotice(String notice) {
		this.notice = notice;
	}

	public Map<Integer, Integer[]> getPriceMap() {
		return priceMap;
	}

	public void setPriceMap(Map<Integer, Integer[]> priceMap) {
		this.priceMap = priceMap;
	}

	public LinkedList<String[]> getMessages() {
		return messages;
	}

	public void setMessages(LinkedList<String[]> messages) {
		this.messages = messages;
	}

	public List<String[]> getSaleRecord() {
		return saleRecord;
	}

	public void setSaleRecord(List<String[]> saleRecord) {
		this.saleRecord = saleRecord;
	}

	public int getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(int beginTime) {
		this.beginTime = beginTime;
	}

}
